

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>


<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/avatar2.jpg">
  <link rel="icon" href="/img/avatar2.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="李延胜">
  <meta name="keywords" content="">
  
    <meta name="description" content="1.格式化输出时间12[root]# echo &#96;date +%Y%m%d-%H:%M:%S&#96;20221027-10:30:56  应用：记录日志，程序执行时间等等 举例：将程序的执行信息输出到指定的文件中 12345#!&#x2F;bin&#x2F;bashlog_file&#x3D;logs&#x2F;clear-dup-data-&#96;date +%Y%m%d-%H:%M:%S&#96;.logecho  start_time-&#96;date +">
<meta property="og:type" content="article">
<meta property="og:title" content="初学shell常用操作">
<meta property="og:url" content="http://example.com/posts/3820.html">
<meta property="og:site_name" content="liyansheng_blog">
<meta property="og:description" content="1.格式化输出时间12[root]# echo &#96;date +%Y%m%d-%H:%M:%S&#96;20221027-10:30:56  应用：记录日志，程序执行时间等等 举例：将程序的执行信息输出到指定的文件中 12345#!&#x2F;bin&#x2F;bashlog_file&#x3D;logs&#x2F;clear-dup-data-&#96;date +%Y%m%d-%H:%M:%S&#96;.logecho  start_time-&#96;date +">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://liyansheng.top/typora/image-20230318180607679.png">
<meta property="article:published_time" content="2022-09-16T12:41:29.000Z">
<meta property="article:modified_time" content="2024-04-28T16:25:54.665Z">
<meta property="article:author" content="李延胜">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://liyansheng.top/typora/image-20230318180607679.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>初学shell常用操作 - liyansheng_blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":50,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":"85b00ecca8bd5efa8a581f758ba73a03","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?85b00ecca8bd5efa8a581f758ba73a03";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>木子-博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>作者</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/remote_help/">
                
                <span>🐞远程调试/功能定做</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tool/">
                
                <span>💪利其器</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HTML/ai-chat.html">
                <i class="iconfont icon-bot"></i>
                <span>🤖Ai助手</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg5.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="初学shell常用操作"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-09-16 20:41" pubdate>
          2022年9月16日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          57 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">初学shell常用操作</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：18 分钟前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h3 id="1-格式化输出时间"><a href="#1-格式化输出时间" class="headerlink" title="1.格式化输出时间"></a>1.格式化输出时间</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">[root]# </span><span class="language-bash"><span class="hljs-built_in">echo</span> `<span class="hljs-built_in">date</span> +%Y%m%d-%H:%M:%S`</span><br>20221027-10:30:56<br></code></pre></td></tr></table></figure>

<p>应用：记录日志，程序执行时间等等</p>
<p>举例：将程序的执行信息输出到指定的文件中</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br>log_file=logs/clear-dup-data-`date +%Y%m%d-%H:%M:%S`.log<br>echo  start_time-`date +%Y%m%d-%H:%M:%S` &amp;&gt;&gt; $log_file<br>/bin/python3 /clean_dup_data/main.py &amp;&gt;&gt; $log_file<br>echo  end_time-`date +%Y%m%d-%H:%M:%S` &amp;&gt;&gt; $log_file<br></code></pre></td></tr></table></figure>

<h3 id="2-命令行操作文本"><a href="#2-命令行操作文本" class="headerlink" title="2.命令行操作文本"></a>2.命令行操作文本</h3><p>对每行匹配到的每一个字符串进行替换</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">sed -i &#x27;s/原字符串/新字符串/&#x27; ab.txt  （&quot;/&quot;也可以换成&quot;#&quot;）<br></code></pre></td></tr></table></figure>

<p>应用：直接搜索修改指定文件的位置，这样你就不用再cd 目录，然后vi 文件，等等这么麻烦了，在脚本中使用特别快捷</p>
<p>举例：使用命令行的形式去修改selinux配置</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">sed -i &#x27;s/SELINUX=enforcing/SELINUX=disabled/&#x27; /etc/selinux/config<br></code></pre></td></tr></table></figure>

<p>指定分割点：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@VM-16-12-centos ~]# hostname -I | awk &#x27;&#123;print $1&#125;&#x27;<br>10.0.16.12<br>[root@VM-16-12-centos ~]# hostname -I | awk &#x27;&#123;print $1&#125;&#x27; | awk -F . &#x27;&#123;print $1&#125;&#x27;<br>10<br></code></pre></td></tr></table></figure>



<h3 id="3-读取键盘输入"><a href="#3-读取键盘输入" class="headerlink" title="3.读取键盘输入"></a>3.读取键盘输入</h3><p>用到<code>read</code>来获取用户的输入 </p>
<p>应用：脚本为了提高与用户的交互，有时候我们希望在脚本运行期间，读取用户的输入，如让用户输入账号，密码等。  </p>
<p>参数说明：</p>
<ul>
<li>-p 提示信息</li>
<li>-t 等待输入秒数</li>
<li>-n 接受指定的字符数便执行</li>
<li>-s 隐藏输入的字符</li>
</ul>
<p>举例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@VM-16-12-centos demo4]# read -p &#x27;enter your name:&#x27; name ; echo $name<br>enter your name:liming<br>liming<br></code></pre></td></tr></table></figure>

<h3 id="4-开发指定端口"><a href="#4-开发指定端口" class="headerlink" title="4.开发指定端口"></a>4.开发指定端口</h3><p><img src="http://liyansheng.top/typora/image-20221028104222903.png" srcset="/img/loading.gif" lazyload alt="image-20221028104222903"></p>
<p>5.查看已开发端口</p>
<p>netstat -ntpl</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng bin]# netstat -ntpl<br>Active Internet connections (only servers)<br>Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    <br>tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      11780/redis-server  <br>tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      9603/sshd           <br>tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      9901/master         <br>tcp6       0      0 :::3306                 :::*                    LISTEN      11707/docker-proxy  <br>tcp6       0      0 :::6379                 :::*                    LISTEN      11780/redis-server  <br>tcp6       0      0 :::8080                 :::*                    LISTEN      11871/docker-proxy  <br>tcp6       0      0 :::2389                 :::*                    LISTEN      11822/docker-proxy  <br>tcp6       0      0 :::22                   :::*                    LISTEN      9603/sshd           <br>tcp6       0      0 ::1:25                  :::*                    LISTEN      9901/master         <br>tcp6       0      0 :::42493                :::*                    LISTEN      28129/java          <br>tcp6       0      0 :::2181                 :::*                    LISTEN      28129/java <br></code></pre></td></tr></table></figure>

<p>开发端口：</p>
<p>firewall-cmd –zone&#x3D;public –add-port&#x3D;9200&#x2F;tcp –permanent</p>
<p>查看端口：</p>
<p>firewall-cmd –list-ports</p>
<h3 id="5-命令行的特殊符号"><a href="#5-命令行的特殊符号" class="headerlink" title="5.命令行的特殊符号"></a>5.命令行的特殊符号</h3><ul>
<li><code>&gt;</code> ：输出重定向，将输出写入指定的文件（如果文件存在，则覆盖；否则创建文件）</li>
<li><code>&gt;&gt;</code>：输出重定向，在指定的文件里面<strong>追加</strong>输出的内容</li>
<li><code>&lt;</code>：输入重定向，后跟的文件取代键盘作为新的输入设备</li>
<li><code>|</code>：管道，将上一条的命令作为下一条命令的输入参数</li>
<li><code>||</code>：上一条命令执行失败后，就执行下一条</li>
<li><code>&amp;</code>：将任务放到后台执行</li>
<li><code>&amp;&amp;</code>：上一条命令执行成功后，继续执行下一条，否则不往下</li>
<li><code>0</code>：stdin 标准输入</li>
<li><code>1</code>：stdout标准正确输出</li>
<li><code>2</code>：stderr标准错误输出</li>
<li><code>2&gt;$1</code>：标准错误重新定向到标准输出</li>
<li><code>1&gt;$2</code>：标准输出重定向到标准错误</li>
<li><code>/dev/null</code>：空设备文件（无底洞）</li>
</ul>
<h3 id="6-虚拟机配置静态IP"><a href="#6-虚拟机配置静态IP" class="headerlink" title="6.虚拟机配置静态IP"></a>6.虚拟机配置静态IP</h3><p>背景：主要解决每次虚拟机开机后，IP就变换了，不利于finalshell连接，每次都得修改IP麻烦。</p>
<p>查看VM的网络配置。</p>
<p>首先VM&#x3D;》编辑 &#x3D;》 虚拟网络编辑器</p>
<p><img src="http://liyansheng.top/typora/image-20221030165854873.png" srcset="/img/loading.gif" lazyload alt="image-20221030165854873"></p>
<p>选择NAT类型这行，然后打开设置</p>
<p><img src="http://liyansheng.top/typora/image-20221030165930597.png" srcset="/img/loading.gif" lazyload alt="image-20221030165930597"></p>
<p>根据下图修改网卡配置文件</p>
<p><img src="http://liyansheng.top/typora/image-20221030165950728.png" srcset="/img/loading.gif" lazyload alt="image-20221030165950728"></p>
<p>修改网卡配置：<code>vi /etc/sysconfig/network-scripts/ifcfg-ens33 </code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs shell">TYPE=&quot;Ethernet&quot;<br>PROXY_METHOD=&quot;none&quot;<br>BROWSER_ONLY=&quot;no&quot; <br>BOOTPROTO=static  #这个配置修改为&quot;static&quot;<br>DEFROUTE=&quot;yes&quot;<br>IPV4_FAILURE_FATAL=&quot;no&quot;<br>IPV6INIT=&quot;yes&quot;<br>IPV6_AUTOCONF=&quot;yes&quot;<br>IPV6_DEFROUTE=&quot;yes&quot;<br>IPV6_FAILURE_FATAL=&quot;no&quot;<br>IPV6_ADDR_GEN_MODE=&quot;stable-privacy&quot;<br>NAME=&quot;ens33&quot;<br>UUID=&quot;38fab940-4871-41f9-b1e1-3f123c3bc217&quot;<br>DEVICE=&quot;ens33&quot;<br>ONBOOT=&quot;yes&quot; #修改为&quot;yes&quot;,代表每次开机后开启<br>IPADDR=192.168.220.137 #你要设置的IP<br>NETMASK=255.255.255.0 #子网掩码<br>GATEWAY=182.168.220.2 #网关<br>DNS1=8.8.8.8 #域名解析器地址<br></code></pre></td></tr></table></figure>

<p>保存，重启网卡：<code>systemctl restart network</code></p>
<p>再次查看IP地址：<code>ip addr</code>，可见ip修改成静态的OK了。</p>
<p>7.添加自启动服务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@zookeeper init.d]# vim zookeeper <br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">chkconfig:2345 20 90</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">description:zookeeper</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">processname:zookeeper</span><br>export JAVA_HOME=/opt/aspire/product/mirror/java/jdk1.8.0_131<br>case $1 in<br>        start) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh start;;<br>        stop) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh stop;;<br>        status) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh status;;<br>        restart) su /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh restart;;<br>        *) echo &quot;require start|stop|status|restart&quot; ;;<br>esac<br></code></pre></td></tr></table></figure>

<h3 id="7-添加开机启动服务"><a href="#7-添加开机启动服务" class="headerlink" title="7.添加开机启动服务"></a>7.添加开机启动服务</h3><blockquote>
<p>方式1：将应用做成服务，以“开机启动zookeeper”为例</p>
</blockquote>
<ol>
<li><p>在<code>/etc/rc.d/init.d/</code>目录下，创建zookeeper服务文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng ~]# cd /etc/rc.d/init.d/<br>[root@liyansheng init.d]# ls<br>functions  netconsole  network  README  zookeeper<br></code></pre></td></tr></table></figure>
</li>
<li><p>将服务文件设置为可执行文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng init.d]# chmod +x zookeeper<br></code></pre></td></tr></table></figure>
</li>
<li><p>编辑服务文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs shell"><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/bash</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">chkconfig:2345 20 90</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">description:zookeeper</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">processname:zookeeper</span><br>export JAVA_HOME=/opt/aspire/product/mirror/java/jdk1.8.0_131<br>case $1 in<br>        start) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh start;;<br>        stop) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh stop;;<br>        status) su root /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh status;;<br>        restart) su /opt/aspire/product/mirror/zookeeper/zookeeper-3.4.10/bin/zkServer.sh restart;;<br>        *) echo &quot;require start|stop|status|restart&quot; ;;<br>esac<br><br></code></pre></td></tr></table></figure>
</li>
<li><p>测试启动</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng init.d]# service zookeeper start<br></code></pre></td></tr></table></figure>

<p>ok的</p>
</li>
<li><p>添加到开机启动</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng init.d]# chkconfig --add zookeeper <br></code></pre></td></tr></table></figure>
</li>
<li><p>查看开机启动列表</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng init.d]# chkconfig --list<br>netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关<br>network         0:关    1:关    2:开    3:开    4:开    5:开    6:关<br>zookeeper       0:关    1:关    2:开    3:开    4:开    5:开    6:关<br></code></pre></td></tr></table></figure>

<blockquote>
<p>方式2：直接修改**&#x2F;etc&#x2F;rc.d&#x2F;rc.local文件**</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng init.d]# vim /etc/rc.d/rc.local <br><span class="hljs-meta prompt_"></span><br><span class="hljs-meta prompt_">#</span><span class="language-bash">!/bin/sh</span><br><span class="hljs-meta prompt_">#</span><span class="language-bash"></span><br><span class="language-bash"><span class="hljs-comment"># This script will be executed *after* all the other init scripts.</span></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash">You can put your own initialization stuff <span class="hljs-keyword">in</span> here <span class="hljs-keyword">if</span> you don<span class="hljs-string">&#x27;t</span></span><br><span class="hljs-meta prompt_"># </span><span class="language-bash"><span class="hljs-string">want to do the full Sys V style init stuff.</span></span><br><br>touch /var/lock/subsys/local<br>export JAVA_HOME=/usr/java/jdk1.8.0_112<br>/usr/local/zookeeper-3.4.5/bin/zkServer.sh start<br></code></pre></td></tr></table></figure>

<p>保存，重启即生效。</p>
</li>
</ol>
<h3 id="8-systemctl-添加开机启动"><a href="#8-systemctl-添加开机启动" class="headerlink" title="8.systemctl 添加开机启动"></a>8.systemctl 添加开机启动</h3><ol>
<li><p>进入目录<code>/lib/systemd/system</code></p>
</li>
<li><p>编辑服务文件<code>xxx.service</code></p>
</li>
<li><p>保存，执行<code>systemctl daemon-reload</code>，使新增的服务生效</p>
</li>
<li><p>设置开机启动<code>systemctl enable xxx.service</code></p>
</li>
<li><p>查看服务状态<code>systemctl status xxx.service</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs shell">[Unit]<br>Description=xxx<br>After=network.target<br><br>[Service]<br>Type=simple<br>PIDFile=<br>ExecStart=/home/gopath/bin/xxx<br>ExecReload=/home/gopath/bin/xxx<br>ExecStop=/home/gopath/bin/xxx<br>RemainAfterExit=yes<br>Restart=no<br>PrivateTmp=true<br>Restart=always<br>RestartSec=1<br>User=root<br>[Install]<br>WantedBy=multi-user.target<br></code></pre></td></tr></table></figure></li>
</ol>
<h3 id="9-超简洁获取IP地址"><a href="#9-超简洁获取IP地址" class="headerlink" title="9.超简洁获取IP地址"></a>9.超简洁获取IP地址</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">hostname -I |awk &#x27;&#123;print $1&#125;&#x27;<br></code></pre></td></tr></table></figure>



<h3 id="10-便捷快速文件传输-scp"><a href="#10-便捷快速文件传输-scp" class="headerlink" title="10.便捷快速文件传输-scp"></a>10.便捷快速文件传输-scp</h3><p>语法如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]<br>            [-J destination] [-l limit] [-o ssh_option] [-P port]<br>            [-S program] source ... target<br></code></pre></td></tr></table></figure>

<ul>
<li><p>本机 &#x3D;》 远程</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">scp local_file remote_username@remote_ip:remote_folder <br>或者 <br>scp local_file remote_username@remote_ip:remote_file <br></code></pre></td></tr></table></figure>
</li>
<li><p>远程 &#x3D;》 本机</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">scp  remote_username@remote_ip:remote_folder local_file<br>或者 <br>scp  remote_username@remote_ip:remote_file local_file<br></code></pre></td></tr></table></figure></li>
</ul>
<p>注意：<br>1.当远程服务器防火墙有特殊限制时，scp要走特殊端口(#scp 命令使用端口号 4588)<br>2.注意所使用的用户是否具有可读取远程服务器相应文件的权限</p>
<h3 id="11-set-e-用法"><a href="#11-set-e-用法" class="headerlink" title="11. set  e 用法"></a>11. set  e 用法</h3><ul>
<li>set -e 之后出现的代码，一但出现返回值非零（脚本出错），整个脚本立即退出。</li>
<li>set +e 则 即使基本有报错，但是仍继续执行下面的脚本。</li>
</ul>
<h3 id="12-z-str"><a href="#12-z-str" class="headerlink" title="12.-z str"></a>12.-z str</h3><p>如果str长度为零为真</p>
<p><img src="http://liyansheng.top/typora/image-20221116122413712.png" srcset="/img/loading.gif" lazyload alt="image-20221116122413712"></p>
<h3 id="13-shift用法"><a href="#13-shift用法" class="headerlink" title="13.shift用法"></a>13.shift用法</h3><p>当传入多个参数时，shift前$1可用，shift后原来的$1不可用，原来的$2变成$1，$#获取参数个数会相应-1</p>
<h3 id="14-source用法"><a href="#14-source用法" class="headerlink" title="14.source用法"></a>14.source用法</h3><ul>
<li>刷新当前环境</li>
<li>source 执行shell脚本</li>
<li>从脚本中导入shell函数</li>
<li>从另一个脚本读取变量</li>
</ul>
<p>15.command -v 参数  ：判断一个命令是否支持</p>
<h2 id="自动化配置"><a href="#自动化配置" class="headerlink" title="自动化配置"></a>自动化配置</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs shell">[root@liyansheng target-config]# cat application-prod.yml | grep brokers | awk &#x27;&#123;print $2&#125;&#x27;<br>10.12.7.116:9092<br><br>[root@liyansheng target-config]# cat application-prod.yml | grep zk-nodes | awk &#x27;&#123;print $2&#125;&#x27;<br>10.12.7.116:2181<br><br>[root@liyansheng target-config]# cat application-prod.yml | grep preferred-networks | awk &#x27;&#123;print $2&#125;&#x27;<br>10.12.7<br><br>[root@liyansheng target-config]# cat application-prod.yml | grep defaultZone | awk &#x27;&#123;print $2&#125;&#x27;| awk &#x27;&#123;print $1&#125;&#x27;<br>http://10.12.7.115:8761/eureka/<br><br></code></pre></td></tr></table></figure>

<h2 id="中间件部署脚本"><a href="#中间件部署脚本" class="headerlink" title="中间件部署脚本"></a>中间件部署脚本</h2><p><a target="_blank" rel="noopener" href="http://120.24.82.106:10880/ljunyfor/docker-middleware">ljunyfor&#x2F;docker-middleware - Gogs</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Linux/" class="category-chain-item">Linux</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Linux/">#Linux</a>
      
        <a href="/tags/shell/">#shell</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>初学shell常用操作</div>
      <div>http://example.com/posts/3820.html</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>李延胜</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年9月16日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/29995.html" title="玩转Linux虚拟机，从安装到实操">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">玩转Linux虚拟机，从安装到实操</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/498.html" title="vue引入三方库ElementUi,Echarts等避坑">
                        <span class="hidden-mobile">vue引入三方库ElementUi,Echarts等避坑</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"SabYreKdzdOiQemY4H7Pzbjr-gzGzoHsz","appKey":"o5EFclbNb5gGIxPipHHkBfg2","path":"window.location.pathname","placeholder":"您想说点什么吗...","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://blog.csdn.net/weixin_44107140" target="_blank" rel="nofollow noopener"> <span>CSDN博客</span></a> <i class="iconfont">|</i> <a href="https://gitee.com/yan-sheng-li" target="_blank" rel="nofollow noopener"><span>Gitee码云</span></a> <i class="iconfont">|</i> <a href="#" target="_blank" rel="nofollow noopener"><span style="color:green">微信公众号：《编程亿点有趣》</span></a> <i class="iconfont">|</i><a target="_blank" href="https://qm.qq.com/cgi-bin/qm/qr?k=NZUoWMzd3PQLWwxRGMiBNYEnVkEdNq__&jump_from=webapi&authKey=kgAofDqUzgwMCSX+UQQwxf837zMeWFGGmo4iIcbgkklW2pdfmVOlxPWAK6sMYMaC"> <img border="0" src="../../img/group.png" srcset="/img/loading.gif" lazyload alt="远程调试-学习交流" title="远程调试-学习交流"></a>
    </div>
  
  
    <div class="statistics">
  
  

  
    
    
    
  
</div>

  
  
  

  <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=298 height=52 src="//music.163.com/outchain/player?type=2&id=2031204435&auto=1&height=32"></iframe>

</div>


  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"superSample":1,"width":200,"height":300,"position":"left","hOffset":0,"vOffset":-50},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
